package com.news.ssh.action;

import java.util.List;

import com.news.service.CategoryService;
import com.news.service.NewsService;
import com.news.ssh.entity.NewsCategory;
import com.news.ssh.entity.NewsDatail;

public class NewsAction extends BaseAction{

	private NewsDatail news;
	private NewsCategory newsCategory;
	private NewsService newsservice;
	private CategoryService categoryservice;
	private Integer id;
	public String create() {
		if (news != null) {
			NewsCategory category = categoryservice.findbyid(news
					.getNewsCategory().getId());
			if (category != null) {
				news.setNewsCategory(category);
				newsservice.create(news);
			}

		}
		return "index";
	}

	public String delete() {
		NewsDatail newsDatail=newsservice.findbyid(id);
		if(newsDatail!=null){
			newsservice.delete(newsDatail);
		}
		
		return "index";
	}

	public String update() {
		NewsDatail newsDatail=newsservice.findbyid(news.getId());
		System.out.println(news.getNewsCategory().getId());
		NewsCategory category=categoryservice.findbyid(news.getNewsCategory().getId());
		if(newsDatail!=null){
			news.setNewsCategory(category);
			newsservice.update(news);
		}
		
		return "index";
	}
	
	public String updatebyid(){
//		HttpServletRequest request = ServletActionContext.getRequest();
		NewsDatail newsDatail= newsservice.findbyid(id);
		List<NewsCategory> allCategory=categoryservice.findall();
		request.setAttribute("allCategory", allCategory);
		request.setAttribute("newsDatail", newsDatail);
		return "update";
	}
	public String findall() {
//		HttpServletRequest request = ServletActionContext.getRequest();
		List<NewsDatail> allNews= newsservice.findall();
		List<NewsCategory> allCategory=categoryservice.findall();
		request.setAttribute("allCategory", allCategory);
		request.setAttribute("allNews", allNews);
		return "list";
	}
	
	public NewsDatail getNews() {
		return news;
	}

	public void setNews(NewsDatail news) {
		this.news = news;
	}

	public NewsService getNewsservice() {
		return newsservice;
	}

	public void setNewsservice(NewsService newsservice) {
		this.newsservice = newsservice;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setCategoryservice(CategoryService categoryservice) {
		this.categoryservice = categoryservice;
	}

	public NewsCategory getNewsCategory() {
		return newsCategory;
	}

	public void setNewsCategory(NewsCategory newsCategory) {
		this.newsCategory = newsCategory;
	}

	

}
